package br.ufrj.cg.controller;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import br.ufrj.cg.model.StampBackgroundImage;
import br.ufrj.cg.model.StampImage;

public class ImageWriter {

	public static void saveImage(StampBackgroundImage stamp)
	{
		File output = null;
		
		String[] splittedPath = stamp.getImagePath().split("\\.");
		
		try {
			
			if(splittedPath[splittedPath.length - 1].equals("png"))
			{
				BufferedImage bf = new BufferedImage(800, 600,
						BufferedImage.TYPE_INT_ARGB);
				Graphics2D graphics = bf.createGraphics();
				graphics.drawImage(stamp.getImage(), 0, 0, 800, 600, null);

				output = new File("output " + System.currentTimeMillis() + ".png");
				ImageIO.write(bf, "png", output);
			}
			else
			{
				if(splittedPath[splittedPath.length - 1].equals("jpg"))
				{
					BufferedImage bf = new BufferedImage(800, 600,
							BufferedImage.TYPE_INT_RGB);
					Graphics2D graphics = bf.createGraphics();
					graphics.drawImage(stamp.getImage(), 0, 0, 800, 600, null);
					
					output = new File("output " + System.currentTimeMillis() + ".jpg");
					ImageIO.write(bf, "jpg", output);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
